//加密平台标识
const OS = 'web';

//加密盐
const SALT = 'emhhHaC';

//上传地址
const UPLOADURL = 'https://api.zhgylgl.com/api/uploadImg';

const appId = 'wxe3ce6dedaeb502a7';
const appSecret = '7e93926d82455b561a446d7e2d05db24';

//工具方法
const tools = {
    getToken() {
        return sessionStorage.getItem('oa-token')
    },
    setToken(token) {
        sessionStorage.setItem('oa-token', token)
    },
    getUserId() {
        return sessionStorage.getItem('oa-userId')
    },
    setUserId(userId) {
        sessionStorage.setItem('oa-userId', userId)
    },
    getUserInfo() {
        return JSON.parse(sessionStorage.getItem('oa-userInfo'))
    },
    setUserInfo(userInfo) {
        sessionStorage.setItem('oa-userInfo', JSON.stringify(userInfo))
    }
};

//获取请求头
function getHeader() {
    const timeStamp = Date.now() + '';
    const sign = '01' + md5(timeStamp + SALT);
    let obj = {
        'os': OS,
        'x-time': timeStamp,
        'x-sign': sign,
        'content-type': 'application/x-www-form-urlencoded; charset=UTF-8' //重点
    };
    if (tools.getToken()) {
        obj['token'] = tools.getToken();
    }
    if (tools.getUserId()) {
        obj['userId'] = tools.getUserId();
    }
    return obj
}

//请求地址前缀
const baseUrl = "https://api.zhgylgl.com/api/admin";
//重写ajax方法
function request(option) {
    return new Promise((resolve, reject) => {
        $.ajax({
            ...option,
            url: option.url.indexOf('http') != -1 ? option.url : (baseUrl + option.url),
            headers: getHeader(),
            success: (res) => {
                if (typeof res == 'string') {
                    resolve(JSON.parse(res))
                } else {
                    resolve(res)
                }
            },
            error: (e) => {
                reject(e)
            }
        })
    })
}

function ajax() {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve({
                code: 200,
                data: '2222222'
            })
        }, 1000)
    })
}

const getUrlParam = (name) => {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = (window.location.search).substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
};